﻿using System.ComponentModel;
using MarginCalc.Commands;
using MarginCalc.Settings;
using Windows.Storage;

namespace MarginCalc.ViewModels
{
    internal class MainViewModel : INotifyPropertyChanged
    {
        private decimal _cost;
        private decimal _finalPrice;
        private decimal _marginPercent;
        private decimal _taxPercent;

        public DelegateCommand CalculateFinalPriceCommand { get; set; }
        public DelegateCommand CalculateMarginPercentCommand { get; set; }
        public DelegateCommand CalculateCostCommand { get; set; }

        public MainViewModel()
        {
            var tp = DecimalSettingsStore.GetDecimalValue("TaxPercent");
            if (tp == null)
                DecimalSettingsStore.SaveDecimal("TaxPercent", 0m);
            decimal taxPc;
            taxPc = tp == null ? 0m : (decimal) tp;
            _taxPercent = taxPc;
            _cost = 0;
            _marginPercent = 0;
            _finalPrice = 0;
            CalculateFinalPriceCommand=new DelegateCommand(t=>
                                                               {
                                                                   FinalPrice = Cost + MarginValue + TaxValue;
                                                               });
            CalculateMarginPercentCommand=new DelegateCommand(t=>
                                                                  {
                                                                      MarginPercent = (FinalPrice - (TaxPercent + 1)*Cost)/((TaxPercent + 1)*Cost);
                                                                  });
            CalculateCostCommand=new DelegateCommand(t=>
                                                         {
                                                             Cost = FinalPrice/((TaxPercent + 1)*MarginPercent + TaxPercent + 1);
                                                         });
        }

        public decimal TaxPercent
        {
            get { return _taxPercent; }
            set
            {
                _taxPercent = value;
                DecimalSettingsStore.SaveDecimal("TaxPercent", value);
                OnPropertyChanged("TaxPercent");
            }
        }

        public decimal TaxValue
        {
            get { return (Cost + MarginValue)*_taxPercent; }
        }

        public decimal Cost
        {
            get { return _cost; }
            set
            {
                _cost = value;
                OnPropertyChanged("Cost");
                OnPropertyChanged("MarginValue");
            }
        }

        public decimal MarginPercent
        {
            get { return _marginPercent; }
            set
            {
                _marginPercent = value;
                OnPropertyChanged("MarginPercent");
                OnPropertyChanged("MarginValue");
            }
        }

        public decimal MarginValue
        {
            get { return _cost*_marginPercent; }
        }

        public decimal FinalPrice
        {
            get { return _finalPrice; }
            set
            {
                _finalPrice = value;
                OnPropertyChanged("FinalPrice");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}